package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.AddressSpot
import com.example.shushiyunsst.util.ADD_DELIVERY_INFO
import com.example.shushiyunsst.util.ADD_DELIVERY_INFO_HISTORY
import com.example.shushiyunsst.util.startNavigation
import com.example.wallet.db.UserInfoManager

class DeliveryInfoFragment : BaseFragmentEx(), View.OnClickListener {

    companion object {
        fun newInstance(): DeliveryInfoFragment {
            val args = Bundle()
            val fragment = DeliveryInfoFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private val user = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

    private lateinit var addAddressForGoods: TextView

    private lateinit var consigneeTv: TextView

    private lateinit var phoneTv: TextView

    private lateinit var deliveryAddress: TextView


    private lateinit var addressLayout: View

    private lateinit var addMore: View

    private var mAddressSpot: AddressSpot? = null

    fun getAddressSpot() = mAddressSpot

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport=false
    }

    override fun getLayoutId(): Any {
        return R.layout.delivery_info_fragment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        addAddressForGoods = view.findViewById(R.id.add_address_for_goods)

        consigneeTv = view.findViewById(R.id.consigneeTv)

        phoneTv = view.findViewById(R.id.phoneTv)

        deliveryAddress = view.findViewById(R.id.delivery_address)


        addressLayout = view.findViewById(R.id.addressLayout)

        addMore = view.findViewById(R.id.addMore)

        addMore.setOnClickListener(this)

        addAddressForGoods.setOnClickListener(this)

        getAddressInfo()
    }

    private fun getAddressInfo() {
        val maps = mapOf(
            "uid" to user?.uid
        )
        RestClient.builder()
            .url(R.string.queryMyAddressList)
            .raw(JSON.toJSONString(maps))
            .success {
                val parse = JSON.parseObject(it)
                val addressList = parse.getJSONArray("addrList")
                if (addressList.isEmpty()) {
                    addressLayout.visibility = View.GONE
                    addAddressForGoods.visibility = View.VISIBLE
                } else {
                    addressLayout.visibility = View.VISIBLE
                    addAddressForGoods.visibility = View.GONE
                    val mAddressSpots =
                        JSON.parseArray(addressList.toJSONString(), AddressSpot::class.java)

                    for (e in mAddressSpots) {
                        if (e.isDefaultSetting) {
                            mAddressSpot = e
                            break
                        }
                    }

                    if (mAddressSpot == null && mAddressSpots.isNotEmpty()) {
                        mAddressSpot = mAddressSpots[0]
                    }
                    setAddressInfo(mAddressSpot)
                }
            }.failure {


            }.error { _, _ ->

            }
            .build()
            .post()
    }

    private fun setAddressInfo(mAddressSpot: AddressSpot?) {
        deliveryAddress.text = mAddressSpot?.recAddr
        consigneeTv.text = mAddressSpot?.recName
        phoneTv.text = mAddressSpot?.recPhone
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.add_address_for_goods -> {
                ARouter.getInstance()
                    .build(ADD_DELIVERY_INFO)
                    .startNavigation(this)
            }

            R.id.addMore -> {
                ARouter.getInstance()
                    .build(ADD_DELIVERY_INFO_HISTORY)
                    .startNavigation(this)
            }
        }
    }

    override fun handMessage(msg: MessageWrap?) {
        super.handMessage(msg)

        if (msg?.message == ADD_DELIVERY_INFO) {
            getAddressInfo()
        }
    }
}